<?php

/**
 * 前台影片管理模块类
 * @author phills.li
 *
 */
class MovieAction extends Action {
    public function index() {        
        $this->display();
    }
   
    /**
     * 收藏影片 
     */
   public function ajaxAddCollectMovie(){
       //判断是否传入参数是否存在
       if(isset($_REQUEST['movie_id']) && isset($_REQUEST['user_id'])){
           //去除传入参数左右两边的空格
           $movie_id = trim($_REQUEST['movie_id']);
           $user_id  = trim($_REQUEST['user_id']);
           $user = M('user_info');

           if(!empty($user_id))
               $map['user_id'] = $user_id;
           $userInfo = $user->where($map)->find();
           $where['id'] = $userInfo['id'];
           $data = array();
           if(!empty($movie_id)){
               if($userInfo['favorite_movie_list'])
                   $data['favorite_movie_list'] = $userInfo['favorite_movie_list'].$movie_id.",";
               else
                   $data['favorite_movie_list'] = ",".$movie_id.",";
           }
           $rows = $user->where($where)->setField($data);
           if($rows)
               $this->ajaxReturn ("", "收藏影片成功!", 1);
           else
               $this->ajaxReturn ("", "收藏影片失败!", 0);
       }
       else{
              $this->ajaxReturn('','请传入必须参数', 0);
       }
   }
   
   /**
    * 取消收藏影片
    */
   public function ajaxCancelCollectMovie(){
       //判断是否传入参数是否存在
       if(isset($_REQUEST['movie_id']) && isset($_REQUEST['user_id'])){
           //去除传入参数左右两边的空格
           $movie_id = trim($_REQUEST['movie_id']);
           $user_id  = trim($_REQUEST['user_id']);
           $user = M('user_info');
           if(!empty($user_id))
               $map['user_id'] = $user_id;
           $userInfo = $user->where($map)->find();
           if(!empty($movie_id)){
               $split = ",";
               $pattern = "/(,$movie_id,)/"; 
               $data['favorite_movie_list'] = preg_replace($pattern, $split, $userInfo['favorite_movie_list']);
               $rows = $user->where($map)->setField($data);
               if($rows)
                   $this->ajaxReturn ("", "取消影片成功!", 1);
               else
                   $this->ajaxReturn ("", "取消影片失败!", 0);
           }
           else
               $this->ajaxReturn ("", "影片不能为空!", 0);
       }
       else
           $this->ajaxReturn('','请传入必须参数', 0);
   } 
}
?>